package com.lmx.web.controller.thirdPart;

import java.io.BufferedOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lmx.persistence.service.HealthInfoService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/*-
 * 处理接口
 */

@Controller
@RequestMapping("/access/etcomm")
public class AllInOneDevController {
	private static Log logger = LogFactory.getLog(AllInOneDevController.class);
	@Autowired
	private HealthInfoService healthInfoService;

	@RequestMapping(path="/generalJsonSync", method = RequestMethod.POST)
	@ResponseBody
	public void generalJsonSync(HttpServletRequest request, HttpServletResponse response) {
		
		logger.debug("-----------------------------获取一体机同步数据接口    AllInOneDevController-------------------------------------");
		String result = request.getParameter("requestData");
		logger.info("获取一体机同步数据 result: " + result);
		logger.debug("allInOneDev0 -----" + request.getParameter("requestData")+" "+request.getRequestURL()+" "+request.getRequestURI());
		if (request.getParameter("datas") != null) {
		    result = request.getParameter("datas");
			JSONArray jsonArray = JSONArray.fromObject(result);
			for (int i = 0; i < jsonArray.size(); i++) { 
				JSONObject object = jsonArray.getJSONObject(i);
				String dataId = object.getString("inspId");
				if (healthInfoService.isHealthInfoExists(dataId)) {
					continue;
				}
				healthInfoService.sychEtcHealthInfo(object);
			}
		}
		BufferedOutputStream out;
		try {
			out = new BufferedOutputStream(response.getOutputStream());
			out.write("http ok".getBytes());
			out.flush();
			out.close();
		} catch (IOException e) {
			logger.debug("generalJsonSync error " + e);
		}
	}
}
